package cn.edu.nju.software.cdiy.dao.impl;

import java.util.List;

import org.hibernate.Query;
import org.hibernate.Session;
import org.hibernate.SessionFactory;
import org.hibernate.Transaction;
import org.springframework.beans.factory.annotation.Autowired;

import cn.edu.nju.software.cdiy.bean.Admin;
import cn.edu.nju.software.cdiy.dao.AdminDao;
public class AdminDaoImpl implements AdminDao {
	
	@Autowired
	private SessionFactory sessionFactory;

	public void setSessionFactory(SessionFactory sessionFactory) {
		this.sessionFactory = sessionFactory;
	}

	public Admin login(String signInName, String password) {

		return this.privatelogin(signInName, password);
	}

	/*
 * 
 */
	public Admin privatelogin(String signInName, String password) {
		Session session = sessionFactory.getCurrentSession();

		Admin returnAdmin = null;
		try {

			Query query = session
					.createQuery("from Admin  a where a.name=:adminname and a.password=:adminpassword");
			query.setString("adminname", signInName);
			query.setString("adminpassword", password);
			returnAdmin = (Admin) query.uniqueResult();

		} catch (Exception e) {

			e.printStackTrace();
		}

		return returnAdmin;
	}

	@Override
	public Admin updateAdmin(Admin admin) {
		Session session = sessionFactory.getCurrentSession();
		Transaction tx1 = null;

		try {
			tx1 = session.beginTransaction();
			session.update(admin);

			tx1.commit();
		} catch (Exception e) {
			if (tx1 != null)
				tx1.rollback();
			e.printStackTrace();
		} 
		return admin;
	}

	@Override
	public Admin updateRegistor(Admin admin) {

		List list = null;
		if (checkExist(admin)) {

			return null;
		} else {
			Session session = sessionFactory.getCurrentSession();
			Transaction tx1 = null;
			Admin returnadmin = null;
			try {
				tx1 = session.beginTransaction();
				session.save(admin);
				// session.load(admin, admin.getId());
				Query query = session
						.createQuery("from Admin  a where a.name=:adminname and a.password=:adminpassword");
				query.setString("adminname", admin.getName());
				query.setString("adminpassword", admin.getPassword());
				list = query.list();
				if (list != null)
					returnadmin = (Admin) list.get(0);

				tx1.commit();

			} catch (Exception e) {
				if (tx1 != null)
					tx1.rollback();
				e.printStackTrace();
			} 
			return returnadmin;

		}

	}

	private boolean checkExist(Admin admin) {

		Session session = sessionFactory.getCurrentSession();
		Transaction tx1 = null;
		boolean returntype = false;
		try {
			tx1 = session.beginTransaction();
			System.out.println(admin.getName());
			Query query = session
					.createQuery("from Admin a where a.name=:adminname");

			query.setString("adminname", admin.getName());

			List list = query.list();

			if (list.size() != 0) {

				returntype = true;

			}
			tx1.commit();
		} catch (Exception e) {
			if (tx1 != null)
				tx1.rollback();
			e.printStackTrace();
		} 
		return returntype;

	}

	

	@Override
	public Admin getAdminById(Long id) {
		Session session = sessionFactory.getCurrentSession();

		Admin returnAdmin = null;
		try {

			returnAdmin = (Admin) session.load(Admin.class, id);

		} catch (Exception e) {

			e.printStackTrace();
		} 
		return returnAdmin;
	}

	@Override
	public void deleteMainBoard(long id) {
		
		Session session = sessionFactory.getCurrentSession();

	
		try {

			Query query=session.createQuery("delete from MainBoard m where m.id=:id");
			query.setLong("id", id);
			query.executeUpdate();

		} catch (Exception e) {

			e.printStackTrace();
		} 
		
	}

}
